/**
 * @Title: index.js
 * @Description: index
 * @author: Fanchao
 * @date: 2020/12/15 6:18 下午
 * @version V1.0
 */
import React, { useEffect } from "react";
import { Table, Button, Popconfirm } from "antd";
import styles from "./index.less";
import SectionTitle from "components/SectionTitle";
import AddSomeButton from "components/AddSomeButton";
import FilterByKeyWord from "components/FilterByKeyWord";
import { connect } from "umi";
import UserModal from "@/pages/evaluations/the-objects-student/components/UserModal";
import storageUtils from "utils/storage";

const TheObjectsStudent = ({ theObjectsStudent: { dataSource, keyword, reqPageInfo, modalTitle, modalVisible }, dispatch, loading }) => {
    const isAdmin = storageUtils.getUser().userRoleTypes.includes("Admin");
    useEffect(() => {
        dispatch({
            type: "theObjectsStudent/getDataSource",
            payload: {
                keyword: "",
                reqPageInfo: {
                    pageNumber: 1,
                    pageSize: 10
                }
            }
        });
    }, []);
    const columns = [
        {
            title: "教学班",
            dataIndex: "classNo",
            align: "left"

        },
        {
            title: "所属学员队",
            dataIndex: "departmentName",
            align: "left"
        },
        {
            title: "课程名称",
            dataIndex: "courseName",
            align: "left"
        },
        {
            title: "授课教员",
            dataIndex: "teacherUserName",
            align: "left"
        },
        {
            title: "学年",
            align: "left",
            dataIndex: "schoolYear"
        },
        {
            title: "学期",
            dataIndex: "semester",
            align: "left"

        },
        isAdmin ? (
            {}
            // {
            //     title: "操作",
            //     align: "left",
            //
            //     render: (item) => {
            //         return (
            //             <div>
            //                 {/*<Button style={{ padding: 2, fontWeight: "bold" }} type="link"*/}
            //                 {/*        onClick={() => updateItem(item)}>修改</Button>*/}
            //                 <Popconfirm title="是否要删除？" onConfirm={() => handleDelete(item)}>
            //                     <Button style={{ padding: 2, color: "#C21217", fontWeight: "bold" }}
            //                             type="link">删除</Button>
            //                 </Popconfirm>
            //             </div>
            //         );
            //     }
            // }
        ) : {}
    ];
    const title = "新增评学对象信息";

    const addItem = (item) => {
        dispatch({
            type: "theObjectsStudent/getTeacherOptionList",
            payload: {}
        });
        dispatch({
            type: "theObjectsStudent/setModalTitle",
            payload: "新增评学对象信息"
        });
        dispatch({
            type: "theObjectsStudent/setModalVisible",
            payload: true
        });
        dispatch({
            type: "theObjectsStudent/setItemData",
            payload: item
        });
    };
    const updateItem = (item) => {
        dispatch({
            type: "theObjectsStudent/setModalTitle",
            payload: "修改评定对象"
        });
        dispatch({
            type: "theObjectsStudent/setModalVisible",
            payload: true
        });
        dispatch({
            type: "theObjectsStudent/setItemData",
            payload: item
        });
    };
    const handleDelete = (item) => {
        dispatch({
            type: "theObjectsStudent/removeEvaluateObject",
            payload: {
                evaluateObjectId: item.evaluateObjectId
            },
            callback: (result) => {
                if (result) {
                    dispatch({
                        type: "theObjectsStudent/getDataSource",
                        payload: {
                            keyword,
                            reqPageInfo: {
                                pageNumber: reqPageInfo.pageNumber,
                                pageSize: reqPageInfo.pageSize
                            }
                        }
                    });
                }
            }
        });
    };
    const searchByKeyWord = (keyword) => {
        dispatch({
            type: "theObjectsStudent/getDataSource",
            payload: {
                keyword,
                reqPageInfo: {
                    pageNumber: reqPageInfo.pageNumber,
                    pageSize: reqPageInfo.pageSize
                }
            }
        });
    };
    const searchByPageNumber = (pageNumber) => {
        dispatch({
            type: "theObjectsStudent/getDataSource",
            payload: {
                keyword,
                reqPageInfo: {
                    pageNumber,
                    pageSize: reqPageInfo.pageSize
                }
            }
        });
    };
    return (
        <div className={styles.pageContainer}>
            {
                isAdmin ? (
                    <div className={styles.pageContainerHeader}>
                        <SectionTitle title={title}/>
                        <div className={styles.btnGroup}>
                            <div style={{ width: "220px" }} onClick={() => addItem({})}>
                                <AddSomeButton title={title}/>
                            </div>
                        </div>
                    </div>
                ) : null
            }

            <div className={styles.pageContainerTable}>
                <SectionTitle title={"已有评定对象"}/>
                <FilterByKeyWord onSearch={keyword => searchByKeyWord(keyword)}
                                 onReset={keyword => searchByKeyWord(keyword)}/>
                <div className={styles.tableContainer}>
                    <Table
                        bordered={false} // 是否有边框
                        rowKey='evaluateObjectId' // 唯一key值
                        loading={loading} // 加载动画
                        columns={columns} // 表头
                        dataSource={dataSource.contentList}
                        pagination={{
                            total: dataSource.totalElements,// 总记录数
                            defaultPageSize: 10, // 默认显示条数
                            showQuickJumper: true, // 快速跳转
                            showSizeChanger: true, // 一页展示数据条数
                            onChange: (pageNumber) => searchByPageNumber(pageNumber),
                            showTotal: () => {
                                return `共${dataSource.totalElements}条`;
                            }
                        }}
                    />
                </div>
            </div>
            <UserModal
                title={modalTitle}
                visible={modalVisible}
            />
        </div>
    );

};

export default connect(({ theObjectsStudent, dispatch, loading }) => ({
    theObjectsStudent,
    dispatch,
    loading: loading.effects["theObjectsStudent/getDataSource"]
}))(TheObjectsStudent);
